<?php
	include_once "lib/User.php";
	include "lib/Kewenangan_user.php";
	include "config/config.php";
	include "lib/Log_user.php";

	$config=new config();
	$config->connect();
	
	$cek_field = "";
	$bool="";
		
	if(isset($_SESSION['nip']) && isset($_SESSION['kewenangan']) && isset($_SESSION['unit'])){

		header("Location:beranda.php");
	}
	
	if(isset($_POST["nip"])){
	$nip=$_POST["nip"];}
	
	if(isset($_POST["password"])){
	$password=$_POST["password"];}
	
	if(isset($_POST["MM_login"])){
		if($nip!="" || $password!="" ){
			$daftar_user = new Daftar_user();
			$bool = $daftar_user->cek_user($nip,$password);
			if ($bool == FALSE){
				echo "<script>
				alert('NIP dan Password tidak ditemukan.')
				</script>";
				
				} else{
					$daftar_user = new Daftar_user();
					$user= $daftar_user->getByNip($nip);
					$kewenangan_user = new Daftar_kewenangan_user();
					$bool1 = $kewenangan_user->cek_kewenangan_user($nip);
					
					if ($bool1 == FALSE){
					echo "<script>
					alert('Pengguna tidak memiliki kewenangan. Silahkan hubungi administrator.')
					</script>";
					
					} else{
					$daftar_kewenangan_user = new Daftar_kewenangan_user();
					$list_kewenangan = $daftar_kewenangan_user->getByNip($nip);
					$kewenangan= $list_kewenangan[0]->getKewenangan();
					
					$unit_kewenangan = $daftar_kewenangan_user->getUnitKewenangan($nip,$kewenangan);
						
					session_start();
					$_SESSION['nip'] = $nip;
					$_SESSION['nama'] = $user->getNama();
					$_SESSION['kewenangan']= $kewenangan;
					$_SESSION['unit']= $unit_kewenangan[0];
					
					
					$log_user = new Log_user();
					$log_user->setNip($nip);
					$waktu_log = date("Y-m-d H:i:s");
					$log_user->setwaktu_log($waktu_log);
					
					$daftar_log_user = new Daftar_log_user();
					$daftar_log_user->add($log_user);
					echo "<script>
							alert('Login berhasil.');
							window.location.href='beranda.php';
						  </script>";
					//header("Location:beranda.php");
								
					
					
						
					}
				}
			
		}
	
	}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SIASKA</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" media="screen" href="css/style.css" />

</head>
<body>

<div align="center" >
		<form name="form" method="post" action="login.php" onsubmit="return cekField();">
		<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
		<font id="p1" >SIASKA</font>
		<br/>
		<font id="p2">Sistem informasi Aliran Surat Kanwil</font>
		
		<table align="center" cellspacing=0 cellpadding=3 height=30% width=40%>
				<tr>
					<td width=10%></td>
					<td><div><br/><br/></div></td>
					<td></td>
					<td></td>
					<td width=10%></td>

				<tr>
				
				<tr>
					<td></td>
					<td>NIP </td>
					<td> : </td>
					<td><input type="text" name="nip" value="" size=32 /></td>
					<td></td>
					<br>
				
                </tr>
        
				<tr>
					<td></td>
					<td>Password </td>
					<td> : </td>
					
					<td><input type="password" name="password" value="" size=32/></td>
					<td></td>
				</tr>
				
				<tr>
					<td></td>
					<td></td>
					<td></td>
					<td></td>
					<td></td>
					<br>
				
                </tr>
				
				<tr>
					<td></td>
					<td></td>
					<td></td>
					<td><div align="right"><input type="submit" name="login" value="Login" ></div></td>
					<td></td>
				</tr>
				<tr>
					<td></td>
					<td><div><br/></div></td>
					<td></td>
					<td></td>
					<td></td>

				<tr>
			</table>
		<input type="hidden" name="MM_login" value="login" >
		</form>
		</div>

<script type="text/javascript">

	function cekField(){
	
		var x = document.forms['form']['nip'].value;
		var y = document.forms['form']['password'].value;

		if(x=="" || y==""){
			alert('Silahkan isi NIP dan password terlebih dahulu.');
			return false;
		}
	}


</script>